package com.inspur.attendance.Service.ServiceImpl;

import com.inspur.attendance.Dao.DailyDao;
import com.inspur.attendance.Entity.Daily;
import com.inspur.attendance.Service.DailyService;
import com.inspur.attendance.Tools.TimeConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional
@Service
public class DailyServiceImpl implements DailyService {

    @Autowired
    DailyDao dailyDao;

    // 获取所有日报
    // @Override
    // public List<Daily> getAllDaily() {
    //
    //     return dailyDao.findAll();
    // }
    public Page<Daily> getAllDaily(int pageNo, int pageSize) {
        Pageable pageable = PageRequest.of(pageNo -1, pageSize);
        return dailyDao.findAll(pageable);
    }

    // 删除日报
    @Override
    public void delDaily(Long id) throws Exception {
        dailyDao.deleteById(id);
    }

    // 添加日报
    @Override
    public void addDaily(Daily daily) throws Exception {
        daily.setCreateTime(new Date());
        dailyDao.save(daily);
    }
    // 添加日报
    public void editDaily(Daily daily) throws Exception {
        dailyDao.save(daily);
    }



    // 查询日期范围中的日报
    @Override
    public Page<Daily> findByDate(String startTime, String endTime, int pageNo, int pageSize) throws Exception {
            Date startDate = TimeConvert.stringToDate(startTime);
            Date endDate = TimeConvert.stringToDate(endTime);
            Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
            return dailyDao.findByDate(startDate, endDate, pageable);
    }

    // 查询员工个人的日报
    @Override
    public List<Daily> findByName(String application) throws Exception {
        return dailyDao.findByApplicant(application);
    }

    @Override
    public Daily findTopByName(String application) throws Exception {
        return dailyDao.findTopByApplicant(application);
    }

    @Override
    public Daily findById(Long id) throws Exception {
        return dailyDao.findTopById(id);
    }
}
